package com.controller.account;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.controller.account.form.AccountForm;
import com.service.account.AccountService;
import com.vo.account.Account;

@Controller
@RequestMapping("/addAccount.htm")
public class AddAccount {
	@Autowired
	private AccountService accountService;

	@RequestMapping(method = RequestMethod.GET)
	public String showAccountForm(ModelMap model) throws Exception {
		AccountForm accountForm = new AccountForm();
		model.put("accountForm", accountForm);
		return "addAccount";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String addAccount(ModelMap model, AccountForm accountForm)
			throws Exception {
		Account account = new Account();
		try {
			BeanUtils.copyProperties(account, accountForm);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		accountService.insertAccount(account);

		return "addAccount";
	}
}
